<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Post\JoinPrimary;
use App\Http\Controllers\Controller;
use App\Models\BookIsbn;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Illuminate\Http\Request;

class BookIsbnController extends Controller
{

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('图书数据')
            ->description('图书基础数据')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new BookIsbn);
        $grid->model()->orderBy('sell_count', 'desc');

        $grid->batchActions(function ($actions) {
            $actions->add(new JoinPrimary());
            $actions->disableDelete();
        });
        $grid->disableActions();
        $grid->disableExport();
        $grid->disableCreateButton();
        $grid->id('ID');
        $grid->isbn13('ISBN');
        $grid->title('书名');
        $grid->author('作者');
        $grid->price_name('价格');
        $grid->sell_count('销售数量');
        $grid->bookisbn()->plan_remind('是否预选')->display(function ($remind) {
            return isset($remind) ? '是' : '否';
        });
        $grid->pubdate('出版时间');
        $grid->publisher('出版社');

        $grid->image('图片')->image(100, 100)->modal(function ($modal) {

            return "<img style='width:100%;' src='" . $this->image . "'>";
        });
        $grid->binding('包装');
        $grid->douban_url('豆瓣地址')->link();
        $grid->created_at('创建时间')->sortable();
        $grid->updated_at('更新时间')->sortable();
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->equal('isbn13', 'ISBN13')->integer();
            $filter->like('title', '书名');

        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(BookIsbn::findOrFail($id));

        $show->id('ID');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }
    /**
     * 处理豆瓣图片
     * @Author   wangwenfan
     * @DateTime 2019-12-04
     * @param    [type]     $url [description]
     * @return   [type]          [description]
     */
    public function doubanImg(Request $request)
    {
        $url = $request->get('url');
        $fullPath = urldecode($url);
        return response()->stream(function () use ($fullPath) {
            echo file_get_contents($fullPath);
        }, 200, ['Content-Type' => 'image/jpeg']);

    }

}
